package com.example.myapplication3;

import android.os.Bundle;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    private EditText etWeight, etHeight;
    private TextView tvResult, tvAdvice;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        etWeight = findViewById(R.id.etWeight);
        etHeight = findViewById(R.id.etHeight);
        Button btnCalculate = findViewById(R.id.btnCalculate);
        tvResult = findViewById(R.id.tvResult);
        tvAdvice = findViewById(R.id.tvAdvice);

       
        btnCalculate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                calculateBMI();
            }
        });
    }

    private void calculateBMI() {
        try {

            double weight = Double.parseDouble(etWeight.getText().toString());
            double height = Double.parseDouble(etHeight.getText().toString()) / 100;
              double bmi = weight / (height * height);
             displayResult(bmi);

        } catch (NumberFormatException e) {
            tvResult.setText("请输入有效的数字");
            tvAdvice.setText("");
        }
    }

    private void displayResult(double bmi) {
        String result = String.format("BMI指数：%.2f", bmi);
        tvResult.setText(result);

        // 健康建议
        String advice;
        int color;
        if (bmi < 18.5) {
            advice ="体重过轻，需要增加营养";
            color = Color.BLUE;
        } else if (bmi < 24.9) {
            advice = "健康体重，继续保持！";
            color = Color.GREEN;
        } else if (bmi < 29.9) {
            advice = "超重，建议适当运动";
            color = Color.YELLOW;
        } else {
            advice = "肥胖，请咨询医生";
            color = Color.RED;
        }

        tvAdvice.setText(advice);
        tvAdvice.setTextColor(color);
    }
}